﻿Imports Microsoft.Office.Core
'Imports Word = Microsoft.Office.Interop.Word
Imports Microsoft.Office.Interop.Outlook
Imports PMOutlookAddIn.UI

Public Class InspectorWrapperCollection
    Inherits DictionaryBase

    Public Overloads Sub Add(ByVal myWrap As InspectorWrapper, ByVal myKey As String)
        MyBase.Dictionary.Add(myKey, myWrap)
    End Sub

    Default Public Property item(ByVal theKey As String) As InspectorWrapper
        Get
            Return CType(MyBase.Dictionary.Item(theKey), InspectorWrapper)
        End Get
        Set(ByVal value As InspectorWrapper)
            MyBase.Dictionary(theKey) = value
        End Set
    End Property

    Public ReadOnly Property keys() As ICollection
        Get
            Return MyBase.Dictionary.Values
        End Get
    End Property

    Public Sub Remove(ByVal theKey As String)
        MyBase.Dictionary.Remove(theKey)
    End Sub
End Class



Public Class InspectorWrapper
    Private WithEvents _thisInsp As Outlook.Inspector
    Private WithEvents _thisMail As Outlook.MailItem
    Private WithEvents _thisContact As Outlook.ContactItem
    'Private WithEvents _cbb As Office.CommandBarButton

    Private WithEvents _cbMoveButton As Office.CommandBarButton
    Private WithEvents _cbExcelButton As Office.CommandBarButton
    Private WithEvents _cbFolderButton As Office.CommandBarButton


    Private _obj As Object
    Private _intID As Integer
    Private _tag As String
    Private _hasMailInspector As Boolean
    Private _hasWordEditor As Boolean


    'Private Sub _cbb_Click1(ByVal Ctrl As Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean) _
    '    Handles _cbb.Click
    '    Dim msgText As String

    '    On Error Resume Next
    '    'code here to handle a click of the menu/toolbar button
    '    ' and perform whatever function you want.
    '    msgText = "You clicked the button on the " & _
    '               _thisInsp.Caption & " window."
    '    MessageBox.Show(msgText, "Command Button Click Event")
    'End Sub

#Region "buttons handling"

    Private Sub _cbFolderButton_Click(ByVal Ctrl As Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean) Handles _cbFolderButton.Click
        Dim selectedMail As Outlook.MailItem = _thisMail ' GetSelectedMail()

        If Not IsNothing(selectedMail) Then
            Dim parentfolder As Outlook.MAPIFolder
            If (TypeOf selectedMail.Parent Is Outlook.MAPIFolder) Then
                parentfolder = CType(selectedMail.Parent, Outlook.MAPIFolder)
                SharedMethods.SetCurrentFolder(Globals.ThisAddIn.Application, parentfolder)
            End If
        End If
    End Sub


    Private Sub _cbExcelButton_Click(ByVal Ctrl As Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean) Handles _cbExcelButton.Click

        Try
            SharedMethods.openDistripostInfofiche()
        Catch ex As System.Exception
            ExceptionHandler.ShowAndLog("Fout bij openen van de Distripost infofiche", ex)
        End Try
    End Sub

    Private Sub _cbMoveButton_Click(ByVal Ctrl As Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean) Handles _cbMoveButton.Click
        Try
            Dim selectedMail As Outlook.MailItem = _thisMail ' GetSelectedMail()
            If selectedMail IsNot Nothing Then
                Using _pmMoveMailForm As pmMoveMailForm = New pmMoveMailForm(Globals.ThisAddIn.Application, _thisMail) ' GetSelectedMail
                    _pmMoveMailForm.ShowDialog()
                End Using
            End If
        Catch ex As System.Exception
            ExceptionHandler.ShowAndLog("Fout bij openen van MoveMailForm", ex)
        End Try
    End Sub
#End Region


    Public Sub New()
        On Error Resume Next

        _thisInsp = Nothing
        _thisMail = Nothing
        _thisContact = Nothing

        _cbExcelButton = Nothing
        _cbMoveButton = Nothing
        _cbFolderButton = Nothing
        '_cbb = Nothing
        _obj = Nothing

        _hasWordEditor = False
    End Sub

    Protected Overrides Sub Finalize()
        On Error Resume Next

        _thisInsp = Nothing
        _thisMail = Nothing
        _thisContact = Nothing

        '_cbb = Nothing
        _cbMoveButton = Nothing
        _cbExcelButton = Nothing
        _cbFolderButton = Nothing
        _obj = Nothing
    End Sub

    Public Function InitButton() As Boolean
        On Error Resume Next

        'if you want buttons only for
        ' certain item types you can test
        ' for that here using
        ' _thisInsp.CurrentItem.Class or TypeOf _thisInsp.CurrentItem
        Call CreateButtons(_thisInsp)

        'you can now enable/disable buttons depending
        ' on what type if item is opened if you want.

    End Function




#Region "InspectorWrapper class properties"
    Public WriteOnly Property MailItem() As Outlook.MailItem
        Set(ByVal value As Outlook.MailItem)
            _thisMail = value
            _hasMailInspector = True
        End Set
    End Property

    Public WriteOnly Property ContactItem() As Outlook.ContactItem
        Set(ByVal value As Outlook.ContactItem)
            _thisContact = value
            _hasMailInspector = False
        End Set
    End Property

    Public Property Inspector() As Outlook.Inspector
        Get
            Inspector = _thisInsp
        End Get
        Set(ByVal value As Outlook.Inspector)
            _thisInsp = value
        End Set
    End Property

    Public Property Key() As Long
        ' key is used to set unique tag for each CommandBarButton
        Get
            Key = _intID
        End Get
        Set(ByVal value As Long)
            _intID = CInt(value)
        End Set
    End Property
#End Region


#Region "Open and Close event handlers for Outlook object properties"

    Private Sub _thisInsp_Close() Handles _thisInsp.Close
        On Error Resume Next

        Me.KillButtons()

        Globals.ThisAddIn.allInspHelper.KillInsp(_intID, Me)
        _thisInsp = Nothing
    End Sub

    Private Sub _thisContact_Close(ByRef Cancel As Boolean) Handles _thisContact.Close
        On Error Resume Next

        'can handle various events for the contact item
        ' in the Inspector like Close and Open.

        If Cancel = False Then
            Me.KillButtons()
            Globals.ThisAddIn.allInspHelper.KillInsp(_intID, Me)
            _thisInsp = Nothing
        End If
    End Sub

    Private Sub _thisContact_Open(ByRef Cancel As Boolean) Handles _thisContact.Open
        On Error Resume Next

        'can handle various events for the contact item
        ' in the Inspector like Close and Open.
    End Sub

    Private Sub _thisMail_Open(ByRef Cancel As Boolean) Handles _thisMail.Open
        On Error Resume Next

        'can handle various events for the mail item
        ' in the Inspector like Close and Open.
    End Sub

    Private Sub _thisMail_Close(ByRef Cancel As Boolean) Handles _thisMail.Close
        On Error Resume Next

        'can handle various events for the mail item
        ' in the Inspector like Close and Open.

        If Cancel = False Then
            Me.KillButtons()
            Globals.ThisAddIn.allInspHelper.KillInsp(_intID, Me)
            _thisInsp = Nothing
        End If
    End Sub


#End Region


#Region "Private procedures to create and remove toolbars,menus, and buttons in the wrapped Inspector"

    Private Sub CreateButtons(ByVal myInsp As Outlook.Inspector)
        On Error Resume Next

        'Adding a new menu item and a button to the main menu for any Inspector
        ' must take a different approach if using Word as email editor.
        If (myInsp.IsWordMail = True) And _
            (myInsp.EditorType = Outlook.OlEditorType.olEditorWord) Then

            _hasWordEditor = True
            _obj = Nothing

            ' #### ADD CODE HERE TO ADD TOOLBAR TO WORD

        Else
            _hasWordEditor = False
            _obj = myInsp

            Me.CreateToolbar()
        End If
        'next maybe wrong
        ''-------------------------
        '_obj = myInsp

        'Me.CreateToolbar()
        ''---------------------

        Err.Clear()
    End Sub

    Private Sub CreateToolbar()
        'add a toolbar to the main Inspector  bar
        ' in the Inspector.
        Dim _tag As String
        Dim myToolBar As Office.CommandBar

        On Error Resume Next

        _tag = "This string is unique to this button" & CStr(_intID)

        'check for the toolbar existing already and do not create
        ' it if does exist. The checking code would set a
        ' Boolean variable named blnToolbarExists.

        myToolBar = _thisInsp.CommandBars("My Bar")
        If myToolBar Is Nothing Then
            'myToolBar = _thisInsp.CommandBars.Add(Name:="My Bar", Temporary:=True)
            myToolBar = _thisInsp.CommandBars.Add("new bar", Office.MsoBarPosition.msoBarTop, False, True)
            '_cbBar = Explorer.CommandBars.Add("PMToolbar", Office.MsoBarPosition.msoBarTop, False, True)
        End If

        ' add the button to the toolbar 

        ' Create movebutton
        _cbMoveButton = CType(myToolBar.Controls.Add(Office.MsoControlType.msoControlButton), Office.CommandBarButton)
        With _cbMoveButton
            .Style = Office.MsoButtonStyle.msoButtonIconAndCaption
            .Caption = "Naar map verplaatsen"
            .FaceId = 1676
        End With

        ' Create folderbutton
        _cbFolderButton = CType(myToolBar.Controls.Add(Office.MsoControlType.msoControlButton), Office.CommandBarButton)
        With _cbFolderButton
            .Style = Office.MsoButtonStyle.msoButtonIconAndCaption
            .Caption = "Bijhorende map openen"
            .FaceId = 1661
        End With

        ' Create excelbutton
        _cbExcelButton = CType(myToolBar.Controls.Add(Office.MsoControlType.msoControlButton), Office.CommandBarButton)
        With _cbExcelButton
            .Style = Office.MsoButtonStyle.msoButtonIconAndCaption
            .Caption = "Distripost reservatie"
            .TooltipText = "Distripost Online reservatietool"
            '.FaceId = 263
            .FaceId = 610
            .BeginGroup = True
        End With

        '_cbb = CType(myToolBar.Controls.Add(Type:=Office.MsoControlType.msoControlButton, _
        '                                  Temporary:=True), CommandBarButton)
        'With _cbb
        '    .Tag = _tag
        '    .Caption = "Test Button"
        '    .TooltipText = "The ToolTip for the button"
        'End With

        myToolBar.Visible = True
        myToolBar = Nothing
    End Sub

    Private Sub KillButtons()
        Dim oControl As Office.CommandBarControl = Nothing

        On Error Resume Next
        ' if you had multiple buttons, you'd need a different tag
        ' property for each one and would delete each one individually
        'oControl = _obj.CommandBars.FindControl(Tag:=_tag)
        oControl = CType(_obj, Inspector).CommandBars.FindControl(Tag:=_tag)

        If Not oControl Is Nothing Then
            oControl.Delete()
        End If

        oControl = Nothing
    End Sub
#End Region

    
End Class
